package com.skyworth.web.wechat.common;

import com.skyworth.web.wechat.protocol.ScanPayReq;
import com.skyworth.web.wechat.protocol.ScanPayRes;
import com.skyworth.web.wechat.protocol.TextMessage;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.core.util.QuickWriter;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;
import com.thoughtworks.xstream.io.xml.XppDriver;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yuhaichao on 2016/8/25.
 */
public class MessageParse {

    public static final String RESP_MESSAGE_TYPE_TEXT = "text";

    public static Map<String, String> parseXml(HttpServletRequest request) {
        try {
            Map<String, String> map = new HashMap<String, String>();

            InputStream inputStream = request.getInputStream();
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputStream);
            Element root = document.getRootElement();
            List<Element> elementList = root.elements();

            for (Element e : elementList) {
                map.put(e.getName(), e.getText());
                System.out.println(e.getName());
                System.out.println(e.getText());
            }

            inputStream.close();
            inputStream = null;

            return map;
        }catch (Exception e){
            return null;
        }
    }

    public static String textMessageToXml(TextMessage textMessage) {
        xstream.alias("xml", textMessage.getClass());
        return xstream.toXML(textMessage);
    }

    public static String notifyReturnMessageToXml(ScanPayRes notifyReturnMessage) {
        xstream.alias("xml", notifyReturnMessage.getClass());
        String notifymsg = xstream.toXML(notifyReturnMessage);
        notifymsg=notifymsg.replaceAll("__", "_");
        return notifymsg;
    }

    public static String payMessageToXml(ScanPayReq payMessage) {
        xstream.alias("xml", payMessage.getClass());
        String paymsg = xstream.toXML(payMessage);
        paymsg=paymsg.replaceAll("__", "_");
        return paymsg;
    }

    public static Map<Object, Object> parseXmlByString(String msg) throws Exception {
        Map<Object, Object> map = new HashMap<>();


        Document document = DocumentHelper.parseText(msg);
        Element root = document.getRootElement();
        List<Element> elementList = root.elements();

        for (Element e : elementList)  {
            map.put(e.getName(), e.getText());
        }

        return map;
    }

    private static XStream xstream = new XStream(new XppDriver() {
        public HierarchicalStreamWriter createWriter(Writer out) {
            return new PrettyPrintWriter(out) {
                boolean cdata = true;

                @SuppressWarnings("unchecked")
                public void startNode(String name, Class clazz) {
                    super.startNode(name, clazz);
                }

                protected void writeText(QuickWriter writer, String text) {
                    if (cdata) {
                        writer.write("<![CDATA[");
                        writer.write(text);
                        writer.write("]]>");
                    } else {
                        writer.write(text);
                    }
                }
            };
        }
    });
}
